package com.zhipu.resetbalance.ui.home.system.device;

import androidx.databinding.ObservableField;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.DeviceUtils;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.NetworkUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.zhipu.resetbalance.mobile.ResultEntity;
import com.zhipu.resetbalance.mobile.temporaryClass.DevCode;
import com.zhipu.resetbalance.remotedata.InterceptObserver;
import com.zhipu.resetbalance.utils.CommonUtils;
import com.zhipu.resetbalance.utils.InitializeUtils;

import java.util.Locale;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class DeviceViewModel extends ViewModel {

    /**
     * 设备编号
     */
    public ObservableField<String> mEquipmentNumber = new ObservableField<>(SPUtils.getInstance().getString("DeviceCode","未获取"));


    /**
     * 当前IP
     */
    public ObservableField<String> mCurrentIP = new ObservableField<>(NetworkUtils.getIPAddress(true));

    /**
     *  版本号
     */
    public ObservableField<String> mAppVersionName = new ObservableField<>("V:" + AppUtils.getAppVersionName());

    /**
     * 自动打印
     */
    public MutableLiveData<Boolean> mAutomaticPrinting = new MutableLiveData<>(SPUtils.getInstance().getBoolean("AutomaticPrinting",false));

    /**
     *  线性定标
     */
    public MutableLiveData<Boolean> mLinearScaling = new  MutableLiveData<>(SPUtils.getInstance().getBoolean("LinearScaling",false));


    /**
     * 获取设备序列号
     */
    public void getDeviceCode(){
        DevCode mDevCode = new DevCode();
        mDevCode.key = DeviceUtils.getAndroidID() + "," + DeviceUtils.getMacAddress().toUpperCase(Locale.getDefault());
        mDevCode.endpointType = "1";
        //时间戳
        long mTimeStamp = TimeUtils.getNowMills();
        String json = GsonUtils.toJson(mDevCode) + "&" + mTimeStamp;
        Observable mObservable = InitializeUtils.getAppComponent().getSystemService().DeviceCode(CommonUtils.toHexString(json),mTimeStamp,mDevCode);
        mObservable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new InterceptObserver<String>() {

            @Override
            public <T> void onNextData(ResultEntity<T> resultEntity) {
                SPUtils.getInstance().put("DeviceCode", resultEntity.message.toString());
                mEquipmentNumber.set(resultEntity.message.toString());
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {
                InitializeUtils.getDefaultMaker().show(t.getMessage() + "___" + netMsg);
            }
        });
    }









    public void shutDown(){
        //
        ActivityUtils.finishActivity(DeviceActivity.class);
    }
}
